package models

import (
	"strconv"

	"github.com/astaxie/beego"

	"github.com/astaxie/beego/orm"
)

type ProjectTeam struct {
	Id        int64 `orm:"column(id)"`
	ProjectId int64 `orm:"column(project_id)"`
	UserId    int64 `orm:"column(user_id)"`
	Role      uint8 // 成员角色: 1-项目经理, 2-审核用户, 3-普通用户
	Created   int64 // 创建时间
}

// TableName 数据库表名
func (p *ProjectTeam) TableName() string {
	return "fh_project_team"
}

func (p *ProjectTeam) String() string {
	return strconv.FormatInt(p.Id, 10)
}

func (p *ProjectTeam) insert() error {
	if _, err := orm.NewOrm().Insert(p); err != nil {
		return err
	}
	return nil
}

func (p *ProjectTeam) update(fields ...string) error {
	_, err := orm.NewOrm().Update(p, fields...)
	if err != nil {
		return err
	}
	return nil
}

func (p *ProjectTeam) read(fields ...string) error {
	err := orm.NewOrm().Read(p, fields...)
	if err != nil {
		return err
	}
	return nil
}

func (p *ProjectTeam) delete() error {
	_, err := orm.NewOrm().Delete(p)
	if err != nil {
		return err
	}
	return nil
}

func (p *ProjectTeam) all() []ProjectTeam {
	projectTeams := make([]ProjectTeam, 0)
	query := orm.NewOrm().QueryTable(p.TableName())
	query.All(&projectTeams)
	return projectTeams
}

func init() {
	orm.RegisterModel(new(ProjectTeam))
}

func CreateUserRight(username, projectName string, role int) error {
	user := User{Username: username}
	err := user.Read("username")
	if err != nil {
		beego.Error(err.Error())
		return err
	}
	project := Project{Name: projectName}
	err = project.Read("name")
	if err != nil {
		beego.Error(err.Error())
		return err
	}

	projectTeam := ProjectTeam{ProjectId: project.Id, UserId: user.Id, Role: uint8(role)}
	err = projectTeam.insert()
	if err != nil {
		beego.Error(err.Error())
	}
	return err
}

type UserRight struct {
	Username string
	Name     string
	Role     uint8
}

func QueryUserRightByUsername(username string) ([]UserRight, error) {
	var rights []UserRight
	qb, _ := orm.NewQueryBuilder("mysql")
	qb.Select("fh_user.username", "fh_project.name", "fh_project_team.role").
		From("fh_project_team").
		LeftJoin("fh_user").On("fh_project_team.user_id = fh_user.id").
		LeftJoin("fh_project").On("fh_project_team.project_id = fh_project.id").
		Where("fh_user.username like ?")
	sql := qb.String()
	o := orm.NewOrm()
	_, err := o.Raw(sql, username+"%").QueryRows(&rights)
	if err != nil {
		beego.Error(err.Error())
	}

	return rights, err
}

func UpdateUserRight(username, projectName string, role int) error {
	user := User{Username: username}
	err := user.Read("username")
	if err != nil {
		beego.Error(err.Error())
		return err
	}
	project := Project{Name: projectName}
	err = project.Read("name")
	if err != nil {
		beego.Error(err.Error())
		return err
	}

	projectTeam := ProjectTeam{ProjectId: project.Id, UserId: user.Id}
	err = projectTeam.read("projectId", "userId")
	if err != nil {
		beego.Error(err.Error())
		return err
	}
	projectTeam.Role = uint8(role)
	err = projectTeam.update("role")
	if err != nil {
		beego.Error(err.Error())
	}
	return err
}
